package cn.com.yusys.yusp.admin.web.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.com.yusys.yusp.admin.domain.AdminSmOrg;
import cn.com.yusys.yusp.admin.service.AdminSmOrgService;
import cn.com.yusys.yusp.commons.exception.MessageConstants;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.util.DateUtil;
import cn.com.yusys.yusp.commons.web.rest.CommonResource;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;

/**
 * 
 * @项目名称: yusp-admin
 * @类名称: AdminSmOrgResource
 * @类描述: 
 * @功能描述: 机构管理resource
 * @创建人: hujun3@yusys.com.cn
 * @创建时间: 2017-12-22 11:29
 * @修改备注: 
 * @修改记录: 修改时间    修改人员    修改原因
 * -------------------------------------------------------------
 * @version 1.0.0
 * @Copyright (c) 2017宇信科技-版权所有
 */
@RestController
@RequestMapping("api/adminsmorg")
public class AdminSmOrgResource extends CommonResource<AdminSmOrg, String> {

	private Logger logger = LoggerFactory.getLogger(AdminSmOrgResource.class);
	
	@Autowired
	private AdminSmOrgService service;

	public AdminSmOrgResource(AdminSmOrgService commonService) {
		super();
		this.service=commonService;
	}

	@Override
	protected CommonService getCommonService() {
		return this.service;
	}
	
	/**
	 * 
	* @方法名称: queryOrgByPage
	* @方法描述: 分页查询
	* @参数与返回说明: 
	* @算法描述:
	 */
	@GetMapping("/querypage")
	public ResultDto<List<Map<String, Object>>> queryOrgByPage(QueryModel queryModel) {
		List<Map<String, Object>> list = service.queryPage(queryModel);
		return new ResultDto<List<Map<String, Object>>>(list);
	}
	/**
	 * 
	* @方法名称: getInstuOrg
	* @方法描述: 自定义码值（金融机构）
	* @参数与返回说明: 
	* @算法描述:
	 */
	@GetMapping("/getinstuorg")
	public ResultDto<List<Map<String, Object>>> getInstuOrg(QueryModel queryModel) {
		List<Map<String, Object>> list = service.getInstuOrg(queryModel);
		return new ResultDto<List<Map<String, Object>>>(list.size(),list);
	}
	
	/**
	 * 
	* @方法名称: addOrgInfo
	* @方法描述: 新增机构数据
	* @参数与返回说明: 
	* @算法描述:
	 */
	@PostMapping("/addorginfo")
	public ResultDto<Object> addOrgInfo(@RequestBody AdminSmOrg pool) {
		int size = service.insertInfo(pool);
		ResultDto<Object> result = new ResultDto<>();
		List<Object> i18nData = new ArrayList<Object>();
		if(size==0) {
			result.setCode(MessageConstants.RET_CODE_20100021);
			result.setMessage("机构代码为："+pool.getOrgCode()+",机构名称为:"+pool.getOrgName()+"的数据保存成功。");
		}else {
			result.setCode(MessageConstants.RET_CODE_20100022);
			result.setMessage("机构代码为："+pool.getOrgCode()+",机构名称为:"+pool.getOrgName()+"的数据已经存在。");
		}
		i18nData.add(pool.getOrgCode());
		i18nData.add(pool.getOrgName());
		result.setI18nData(i18nData);
		result.setData(size);
		return result;
	}
	
	/**
	 * 
	* @方法名称: useBatch
	* @方法描述: 批量启用
	* @参数与返回说明: ids 数据ID主键
	* @算法描述:
	 */
	@PostMapping("/usebatch")
	public ResultDto<Object> useBatch( String ids,String lastChgUsr) {
		int n=0;
		Map<String,Object> result=new HashMap<String, Object>();
		ResultDto<Object> resultdto = new ResultDto<>();
		if(ids !=null&&!"".equals(ids.toString())) {
			String[] idStr=ids.toString().split(",");
			for(int i=0;i<idStr.length;i++) {
				if(!"".equals(idStr[i])) {
					n=n+this.service.useIngFn(idStr[i],lastChgUsr);
				}
			}
			logger.info("Batch enable organizations [PK:{"+ids+"}] "+ DateUtil.formatDateTimeByDef());
			result.put("message", "成功启用"+n+"条数据!");
		}
		resultdto.setMessage((String) result.get("message"));
		resultdto.setData(result);
		return resultdto;
	}
	
	/**
	 * 
	* @方法名称: unUseBatch
	* @方法描述: 批量停用
	* @参数与返回说明: ids 数据ID主键
	* @算法描述:
	 */
	@PostMapping("/unusebatch")
	public ResultDto<Object> unUseBatch(String ids,String lastChgUsr) {
		int n=0;
        List<Object> i18nData = new ArrayList<Object>();
		ResultDto<Object> resultdto = new ResultDto<>();
        int count = 0;
		if(ids !=null&&!"".equals(ids.toString())) {
			String[] idStr=ids.toString().split(",");
			for(int i=0;i<idStr.length;i++) {
				if(!"".equals(idStr[i])) {
                    int s=this.service.queryByOrgId(idStr[i]);
                    int t=this.service.queryRelByOrgId(idStr[i]);
                    if(s>0||t>0) {
                        count++;
                    }else{
                        n=n+this.service.unUseIngFn(idStr[i],lastChgUsr);
                    }

				}
			}
            if(count ==0) {
//                result.put("message", "成功停用"+n+"条数据!");
            	resultdto.setCode(0);
                resultdto.setMessage("成功停用"+n+"条数据!");
                i18nData.add(n);
            }else {
//                result.put("message", "成功停用"+n+"条数据,有"+count+"条数据由于有关联数据无法停用!");
            	resultdto.setCode(0);
                resultdto.setMessage("成功停用"+n+"条数据,有"+count+"条数据由于有关联数据无法停用!");
                i18nData.add(n);
                i18nData.add(count);
            }

		}
		resultdto.setI18nData(i18nData);
		return resultdto;
	}
	
	/**
	 * 
	* @方法名称: deleteBatch
	* @方法描述: 批量删除
	* @参数与返回说明: ids 数据ID主键
	* @算法描述:
	 */
	@PostMapping("/deletebatch")
	public ResultDto<Object> deleteBatch(String ids) {
		int n=0;
		List<Object> i18nData = new ArrayList<Object>();
		ResultDto<Object> resultdto = new ResultDto<>();
		String deletes="";
		int unDeletes=0;
		if(ids !=null&&!"".equals(ids.toString())) {
			String[] idStr=ids.toString().split(",");
			for(int i=0;i<idStr.length;i++) {
				if(!"".equals(idStr[i])) {
					int s=this.service.queryByOrgId(idStr[i]);
					int t=this.service.queryRelByOrgId(idStr[i]);
					if(s>0||t>0) {
						unDeletes++;
					}else {
						n=n+this.service.deleteByPrimaryKey(idStr[i]);
						deletes=deletes+idStr[i];
					}
					
				}
			}
			logger.info("Batch delete organizations [PK:{"+deletes+"}] "+ DateUtil.formatDateTimeByDef());
			if(unDeletes ==0) {
//				result.put("code","200");
//				result.put("message", "成功删除"+n+"条数据!");
				resultdto.setCode(0);
                resultdto.setMessage("成功停用"+n+"条数据!");
                i18nData.add(n);
			}else {
//				result.put("code","2");
//				result.put("message", "成功删除"+n+"条数据,有"+unDeletes+"条数据由于有关联数据无法删除!");
				resultdto.setCode(0);
                resultdto.setMessage("成功删除"+n+"条数据,有"+unDeletes+"条数据由于有关联数据无法删除!");
                i18nData.add(n);
                i18nData.add(unDeletes);
			}
		}
		resultdto.setI18nData(i18nData);
		return resultdto;
	}
	
	/**
	 * 
	* @方法名称: orgTreeQuery
	* @方法描述:  机构树查询
	* @参数与返回说明: 
	* @算法描述:
	 */
	@GetMapping("/orgtreequery")
	public ResultDto<Object> orgTreeQuery(@RequestParam(required = false) String orgId,
			@RequestParam(required = false) String orgSts,@RequestParam(required = false) Boolean lazy) {
		logger.info("Query organization tree");
		List<Map<String, Object>> list=service.getOrgTree(orgId,orgSts,lazy);

		return new ResultDto<Object>(list.size(),list);
	}

}
